import { setStore, getStore, removeStore } from './storage'

export const setDictData = (val) => {
  setStore('DICT_DATA', val)
}
export const getDictData = () => {
  return getStore('DICT_DATA')
}
export const removeDictData = () => {
  removeStore('DICT_DATA')
}
export const getDictByKey = (type, key) => {
  const data = getDictData()
  if (!data) {
    return key ? '' : []
  }
  const jsonData = JSON.parse(data)
  const typeData = jsonData[type] || []
  typeData.forEach(v => {
    v.text = v.dictLabel
    v.value = v.dictValue
  })
  if (key) {
    const finder = typeData.find(v => v.dictValue === key)
    return (finder && finder.dictLabel) || ''
  }
  return typeData

}
export const getDictMapByKey = (type, key) => {
  const data = getDictData()
  if (!data) {
    return key ? key : []
  }
  const jsonData = JSON.parse(data)
  const typeData = jsonData[type] || []
  const valMap = {}
  typeData.forEach(v => {
    valMap[v.dictValue] = v.dictLabel
  })
  if (key) {
    return valMap[key] || key
  }
  return valMap

}